package com.heima.search.service.impl;

import com.heima.common.dto.ResponseResult;
import com.heima.search.dto.ApArticleSearchDto;
import com.heima.search.entity.ApAssociateWords;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ApAssociateWordsServiceImpl {


    @Autowired
    private MongoTemplate mongoTemplate;

    public ResponseResult searchAssociate(ApArticleSearchDto dto) {
        if(StringUtils.isBlank(dto.getSearchWords())){
            return null;
        }

        Query query = new Query(Criteria.where("associateWords").regex(dto.getSearchWords()));
        query.skip(0);
        query.limit(dto.getSize());

        List<ApAssociateWords> apAssociateWords = mongoTemplate.find(query, ApAssociateWords.class);


        return ResponseResult.okResult(apAssociateWords);



    }
}
